#include "canvas.h"

Canvas::Canvas(QList<QSharedPointer<CSGNode> > *nodes)
{
    m_width = 255;
    m_height = 255;

    m_image = new QImage(m_width,m_height,QImage::Format_RGB32);
    m_nodes_ptr = nodes;
    m_ray_caster = new RayCaster();
}

Canvas::~Canvas()
{
    delete m_ray_caster;
    delete m_image;
}

void Canvas::update_image()
{
    for(int x = -m_width/2; x <= m_width/2; x++){
        for(int y = -m_height/2; y <= m_height/2; y++){
            if(m_nodes_ptr->size() > 0){
                m_image->setPixel(x+m_width/2,m_height/2 - y,m_ray_caster->get_point_color(x/(float)m_width,y/(float)m_height,m_nodes_ptr).rgb());
            }else{
                m_image->setPixel(x+m_width/2,m_height/2 - y,QColor(0.0,0.0,0.0).rgb());
            }
        }
    }
    m_image->save("result.png");
}

QImage* Canvas::get_image()
{
    return m_image;
}

unsigned char* Canvas::get_bits()
{
    return m_image->bits();
}

void Canvas::set_nodes(QList<QSharedPointer<CSGNode> > *nodes_ptr)
{
    m_nodes_ptr = nodes_ptr;
}


void Canvas::move_observer(glm::vec3 move_dir)
{
    m_ray_caster->move(move_dir);
}

void Canvas::rotate_view(glm::vec3 rotate_vec, float angle)
{
    m_ray_caster->rotate(rotate_vec, angle);
}
